//隐藏Windows上的控制台窗口
#![windows_subsystem = "windows"]

use components::basic::page::ApplicationPage;
use components::index::index::IndexManager;
use components::resource::resource::ResourceManager;
use components::user::user_manager::UserManager;
use serde_json::value::Index;

use crate::components::login::login::Login;

mod components;

#[cfg(not(target_arch = "wasm32"))]
pub fn start() -> Result<(), eframe::Error> {
    let viewport = egui::ViewportBuilder::default()
        .with_title("XXX平台")
        .with_min_inner_size([200.0, 300.0]);
    // 创建视口选项，设置视口的内部大小为320x240像素
    let options = eframe::NativeOptions {
        viewport,
        ..Default::default()
    };

    // 运行egui应用程序
    eframe::run_native(
        "XXX  平台",
        options,
        Box::new(|cc| {
            setup_custom_fonts(&cc.egui_ctx);
            Ok(Box::new(Application::new(cc)))
        }),
    )
}

#[derive(Clone, Debug)]
pub enum ApplicationState {
    LOGIN,
    INDEX(IndexState),
}
#[derive(Clone, Debug)]
pub enum IndexState {
    IndexState,
    USER,
    RESOURCE,
}

//定义 MyApp 结构体
#[derive(Clone)]
struct Application {
    state: ApplicationState,
}

//MyApp 结构体 new 函数
impl Application {
    fn new(cc: &eframe::CreationContext<'_>) -> Self {
        Self {
            state: ApplicationState::LOGIN,
        }
    }
}

impl eframe::App for Application {
    fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
        match &mut self.state {
            ApplicationState::LOGIN => {
                let mut login = Login::new();
                login.show(ctx);
                // if login.login_result {
                //     self.state = ApplicationState::INDEX(IndexState::IndexState);
                // }
            }
            ApplicationState::INDEX(index_state) => match index_state {
                IndexState::IndexState => {
                    let mut index = IndexManager::default();
                    index.show(ctx);
                    self.state = ApplicationState::INDEX(index_state.to_owned());
                }
                IndexState::USER => {
                    let mut index = IndexManager::default();
                    index.show(ctx);
                    let mut user = UserManager::default();
                    user.show(ctx);
                }
                IndexState::RESOURCE => {
                    let mut index = IndexManager::default();
                    index.show(ctx);
                    let mut resource = ResourceManager::default();
                    resource.show(ctx);
                }
            },
        }
    }
}

//自定义字体
fn setup_custom_fonts(ctx: &egui::Context) {
    // 创建一个默认的字体定义对象
    let mut fonts = egui::FontDefinitions::default();

    //安装的字体支持.ttf和.otf文件
    //文件放在main.rs的同级目录下（可以自定义到其它目录）
    fonts.font_data.insert(
        "my_font".to_owned(),
        egui::FontData::from_static(include_bytes!("../assets/simsun.ttc")),
    );

    // 将字体添加到 Proportional 字体族的第一个位置
    fonts
        .families
        .entry(egui::FontFamily::Proportional)
        .or_default()
        .insert(0, "my_font".to_owned());

    // 将字体添加到 Monospace 字体族的末尾
    fonts
        .families
        .entry(egui::FontFamily::Monospace)
        .or_default()
        .push("my_font".to_owned());

    // 将加载的字体设置到 egui 的上下文中
    ctx.set_fonts(fonts);
}
